package com.example.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
public class LogAspect {

    @Pointcut("@annotation(com.example.annotation.Log)")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint) {

        Object[] args = joinPoint.getArgs();
        String name = joinPoint.getSignature().getName();
        String s = joinPoint.getTarget().toString();

        System.out.println("Before ::::: Target：" + s + "，执行方法：" + name + "，参数个数：" + args.length);
    }

    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        String name = joinPoint.getSignature().getName();
        String s = joinPoint.getTarget().toString();
        System.out.println("After ::::: Target：" + s + "，执行方法：" + name + "，参数个数：" + args.length);
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void logReturn(JoinPoint joinPoint, Object result) {
        String name = joinPoint.getSignature().getName();
        String s = joinPoint.getTarget().toString();
        System.out.println("AfterReturning ::::: Target：" + s + "，执行方法：" + name + "，返回值：" + result);
    }

    @AfterThrowing(value = "pointCut()", throwing = "e")
    public void logThrowing(JoinPoint joinPoint, Exception e) {
        String name = joinPoint.getSignature().getName();
        String s = joinPoint.getTarget().toString();
        System.out.println("AfterReturning ::::: Target：" + s + "，执行方法：" + name + "，一次：" + e);
    }

}
